<?php
// namespace Think\Lib\Util\Alioss;
/**
 * @name 阿里云储存上传驱动
 * @author 娃娃脾气
 * 
 *
 */
use Aliyun\OSS\OSSClient;
require_once 'lib/aliyun.php';

class Alioss{
	
	
    private $rootPath;
    
    private $config;
    
    private $error = ''; //上传错误信息

    private $aliyun;
    
    /**
     * 构造函数，用于设置上传根路径
     */

    public function __construct($configs = array()){
    	$this->config = $configs;
    	$this ->aliyun = OSSClient::factory(array('AccessKeyId' =>$this->config['Alioss']['access_key_id'],'AccessKeySecret' => $this->config['Alioss']['access_key_secret'],'Endpoint' => 'http://oss-cn-hangzhou.aliyuncs.com'));
    }



    /**
     * 保存指定文件
     * @param  array   $file    保存的文件信息
     * @param  boolean $replace 同名文件是否覆盖
     * @return boolean          保存状态，true-成功，false-失败
     */

    public function uploadimg($filename,$fh,$size){
        $result = $this->aliyun -> putObject(array(
                'Bucket' => $this->config['Alioss']['bucket_name'],
                'Key' => "images/".$filename,
                'Content' => $fh,
                'ContentLength' => $size,
        ));

        return $result;
    }

    public function deleteimg($filename){
        $result = $this->aliyun -> deleteObject(array(
                'Bucket' => $this->config['Alioss']['bucket_name'],
                'Key' => $filename,
        ));

        return $result;
    }


    public function getimglist(){
        $objectListing = $this->aliyun->listObjects(array('Bucket' => $this->config['Alioss']['bucket_name'],));

        foreach ($objectListing->getObjectSummarys() as $objectSummary) {
            echo $objectSummary->getKey()."\n";
        }
    }


    // public function delimgall(){
    //     $objectListing = $this->aliyun->listObjects(array('Bucket' => $this->config['Alioss']['bucket_name'],));

    //     foreach ($objectListing->getObjectSummarys() as $objectSummary) {
    //         if($objectSummary->getKey()!="avatar.jpg"){
    //             $this->deleteimg($objectSummary->getKey());
    //         }
            
    //     }
    // }
    
    public function checkRootPath($rootpath){
    	return true;
    }
    public function checkSavePath($savepath){
    	return true;
    }
    public function mkdir($savepath){
    	return true;
    }
    public function getError(){
    	return '';
    }



}
